import io, sys
import os

from lexer import WendLexer
from parser import WendParser
from analyzer import build_symtable
from transasm import  translate_to_asm

def main() :
    if len(sys.argv) != 2:
        sys.exit('Usage: 请输入要运行的Wend文件路径')
    try:
         file_path = os.path.join(os.getcwd(), sys.argv[1])
         f = open(file_path, 'r')
         tokens = WendLexer().tokenize(f.read())
         # current_tokens = tokens.__next__()
         print("Tokens: " , tokens)
         ast = WendParser().parse(tokens )  # 解析语法树
         print("AST: ", ast)
         build_symtable(ast)  # 建立符号表
         print(translate_to_asm(ast))  # 输出汇编代码
    except Exception as e:
        print(e)


if __name__ == '__main__':
    main()

